/*
 * Animatable.h
 *
 *  Created on: 2009/01/25
 *      Author: Yongshik Cho
 */

#ifndef ANIMATABLE_H_
#define ANIMATABLE_H_

#include <vector>

#include "../core/Prerequisites.h"
#include "../core/ISmartPtr.h"
#include "Tickable.h"
#include "../event/AnimationEvent.h"



class Rect2D;

class AnimationFrame
{
public:
	AnimationFrame(const uint32 time, const Rect2D& rect);
	AnimationFrame(const AnimationFrame& rhs);
	~AnimationFrame();

public:
	inline const Rect2D* getRect() const { return mRect; }
	inline const uint32 getTime() const { return mTime; }
	const bool operator == (const AnimationFrame& rhs) const;
	const bool operator != (const AnimationFrame& rhs) const;

private:
	uint32	mTime;
	Rect2D*	mRect;
};

class Animatable : public Tickable
{
	/*	Animation target
	 * 	- position
	 * 	- rectangle size
	 * 	- flip
	 *  - pixel blending
	 */

public:
	enum LOOP_TYPE
	{
		LOOP_COUNT,
		ENDLESS
	};

public:
	Animatable();
	virtual ~Animatable();

public:
	void addFrame(const AnimationFrame& frame);
	void subFrame(const AnimationFrame* frame);
	const uint32 getFrameCount() const { return (uint32)mFrames.size(); }

	inline AnimationFrame* getFrame(const uint32 index)
	{
		if(index >= mFrames.size())
			return 0;

		return &mFrames[index];
	}
	inline AnimationFrame* getFrame()
	{
		if(mIndex >= mFrames.size())
			return 0;

		return &mFrames[mIndex];
	}

	inline const uint32 getIndex() const { return mIndex; }
	inline void setIndex(const uint32 index)
	{
		if(index >= mFrames.size())
			return;

		mIndex = index;
	}

	AnimationFrame* getNextFrame(const uint32 time);

	inline const uint32 getLoopCount() const { return mLoopCount; }
	inline void setLoopCount(const uint32 count) { mLoopCount = count; }
	inline const uint8 getLoopType() const { return mLoopType; }
	inline void setLoopType(const uint8 loopType) { mLoopType = loopType; }

	void animate();

	const bool operator == (Animatable& rhs);
	const bool operator != (Animatable& rhs);

private:
	void onTick(const Event& e);

private:
	vector<AnimationFrame>		mFrames;
	uint32						mIndex;
	Tickable*					mTickable;
	uint8						mLoopType;
	uint32						mLoopCount;
};

#endif /* ANIMATABLE_H_ */
